package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.pojo.Employees;
import com.bwie.pojo.FutureWeather;
import com.bwie.pojo.Realtimeweather;
import com.bwie.pojo.TodayWeather;
import com.bwie.service.EmployeesService;
import com.bwie.service.FutureWeatherService;
import com.bwie.service.RealtimeweatherService;
import com.bwie.service.TodayWeatherService;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WeatherController
 * @Description TODO
 * @Author {涛}
 * @Date 2024/11/8 8:56
 * @Version 1.0
 */
@RestController
@RequestMapping("/weather")
//@CrossOrigin(origins = "http://localhost:8888")
public class WeatherController {

    @Autowired
    private RealtimeweatherService realtimeweatherService;

    @Autowired
    private TodayWeatherService todayWeatherService;

    @Autowired
    private FutureWeatherService futureWeatherService;

    @Autowired
    private EmployeesService employeesService;


    @PostMapping("/analyze")
    public R bachInsert(@RequestBody Map<String, Object> map){
        Map<String, Object> result = (Map<String, Object>) map.get("result");
        Realtimeweather realtimeweather = JSON.parseObject(JSON.toJSONString(result.get("sk")), Realtimeweather.class);
        realtimeweatherService.save(realtimeweather);
        TodayWeather todayWeather = JSON.parseObject(JSON.toJSONString(result.get("today")), TodayWeather.class);
        todayWeatherService.save(todayWeather);
        List<FutureWeather> futureWeathers = JSON.parseObject(JSON.toJSONString(result.get("future")), new TypeReference<List<FutureWeather>>() {});
        futureWeatherService.saveBatch(futureWeathers);
        return R.OK();
    }


    @GetMapping("getWeather")
    public R getWeather() {
        // 根据日期查询今日天气数据
        LambdaQueryWrapper<TodayWeather> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TodayWeather::getDateY);
        TodayWeather todayWeather = todayWeatherService.getOne(queryWrapper);
        // 查询未来天气数据
        List<FutureWeather> futureWeathers = futureWeatherService.list();

        // 创建返回的结果对象
        Map<String, Object> result = new HashMap<>();
        result.put("today", todayWeather);
        result.put("future", futureWeathers);

        return R.OK(result);
    }




    @GetMapping  ("/Empllist")
    public List<Map<String, Object>> getEmployeeSchedules() {
        return employeesService.getEmployeeSchedules();
    }




}


